//
//  TL_FeedbackQuestionChooseView.m
//  WXReader
//
//  Created by TSL on 2021/7/6.
//  Copyright © 2021 Andrew. All rights reserved.
//

#import "TL_FeedbackQuestionChooseView.h"
//#import "UIView+Corner.h"
#import "UIView+LayoutCallback.h"
#import "TL_FeedbackQuestionChooseListTableViewCell.h"

@interface TL_FeedbackQuestionChooseView() <UITableViewDelegate, UITableViewDataSource, UIGestureRecognizerDelegate>

@property (nonatomic, strong) UIView *chooseBackView;

@property (nonatomic, strong) UITableView *chooseTableView;

@end

@implementation TL_FeedbackQuestionChooseView

- (instancetype)init
{
    if (self = [super init]) {
        [self createSubviews];
    }
    return self;
}

- (void)createSubviews
{
    self.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
    self.backgroundColor = UIColor.clearColor;
    [kMainWindow addSubview:self];
    
    UITapGestureRecognizer *dismissTapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(closeAlertView)];
    dismissTapGesture.numberOfTapsRequired = 1;
    dismissTapGesture.delegate = self;
    [self addGestureRecognizer:dismissTapGesture];
    
    self.chooseBackView = [TL_UIFactory viewWithBackgroundColor:kWhiteColor cornerRadius:6];
    self.chooseBackView.layer.shadowColor = kBlackColor.CGColor;
    self.chooseBackView.layer.shadowOffset = CGSizeMake(0, 0);
    self.chooseBackView.layer.shadowOpacity = 0.1f;
    self.chooseBackView.layer.shadowRadius = 10.0f;
    self.chooseBackView.clipsToBounds = NO;
    [self addSubview:self.chooseBackView];
    
    self.chooseTableView = [TL_UIFactory tableViewWithBackgroundColor:UIColor.clearColor delegate:self style:UITableViewStylePlain cellClass:@[TL_FeedbackQuestionChooseListTableViewCell.class]];
    [self addSubview:self.chooseTableView];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.feedbackCenterModel.help_list.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    TL_FeedbackQuestionChooseListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass(TL_FeedbackQuestionChooseListTableViewCell.class) forIndexPath:indexPath];
    if (cell == nil) {
        cell = [[TL_FeedbackQuestionChooseListTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:NSStringFromClass(TL_FeedbackQuestionChooseListTableViewCell.class)];
    }
    TL_HelpListModel *helpListModel = [self.feedbackCenterModel.help_list wx_objectOrNilAtIndex:indexPath.row];
    cell.questionContent = helpListModel.name;
    cell.hiddenEndLine = (indexPath.row == self.feedbackCenterModel.help_list.count - 1);
    cell.backgroundColor = UIColor.clearColor;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 40;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    TL_HelpListModel *helpListModel = [self.feedbackCenterModel.help_list wx_objectOrNilAtIndex:indexPath.row];

    if (self.chooseQuestionBlock) {
        self.chooseQuestionBlock(helpListModel, indexPath.row);
    }
    [self closeAlertView];
}

- (void)setFeedbackCenterModel:(TL_FeedbackCenterModel *)feedbackCenterModel
{
    _feedbackCenterModel = feedbackCenterModel;
    
    [self.chooseTableView reloadData];
}

- (void)showChooseViewFromOriginY:(CGFloat)originY
{
    [self.chooseTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(kMoreHalfMargin);
        make.right.mas_equalTo(self.mas_right).with.offset(- kMoreHalfMargin);
        make.top.mas_equalTo(originY);
        make.height.mas_equalTo(CGFLOAT_MIN);
    }];
    
    [self.chooseBackView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(self.chooseTableView);
    }];
    
    CGFloat chooseViewHeight = 40 * self.feedbackCenterModel.help_list.count;
    if (self.feedbackCenterModel.help_list.count > 3) {
        chooseViewHeight = 40 * 4;
    }
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [UIView animateWithDuration:kAnimatedDuration animations:^{
            [self.chooseTableView mas_updateConstraints:^(MASConstraintMaker *make) {
                make.height.mas_equalTo(chooseViewHeight);
            }];
            [self.chooseTableView.superview layoutIfNeeded];
        }];        
    });
}

- (void)closeAlertView
{
    
    [UIView animateWithDuration:kAnimatedDuration animations:^{
        [self.chooseTableView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(CGFLOAT_MIN);
        }];
        [self.chooseTableView.superview layoutIfNeeded];
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
    if ([touch.view isEqual:self.chooseBackView] || [touch.view isEqual:self]) {
        return YES;
    } else {
        return NO;
    }
}

@end
